<?php
namespace app\kucun\controller;

use app\kucun\model\ProductStockLogModel;
use app\kucun\model\ProductStockModel;
use app\kucun\model\WxWorkUserModel;

/**
 * Class Index
 * @package app\index\controller
 */
class ProductStock extends Base
{
    public function add(){


        $this->checkUserLogin();

        $product_id = input('product_id', 0, 'intval');
        $code = input('code', "", 'string');
        $id = input('id', 0, 'intval');

        $option_type = input("option_type", 0, 'intval');

        if(!in_array($option_type, [1, 2, 3])) return $this->returnError([], '操作有误');
        if(empty($code)){
            return $this->returnError([], '库存编码不能为空');
        }
        $this->user = WxWorkUserModel::where('id', $this->user['id'])->find();
        if($this->user['shop_id'] == 0){
            return $this->returnError([], '请先给用户分配门店');
        }
        $product_stock = ProductStockModel::where('code', $code)->find();
        //  记录操作日志。
        $stock_id = 0;
        $option_id = 0;
        $option_uid = $this->user['id'];
        $option_shop_id = $this->user['shop_id'];
        if($option_type == 1){
            if($product_stock){
                if($product_stock->status == ProductStockModel::STOCK_STATUS_IN_SHOP){
                    return $this->returnError([], '该库存已在库中，无需重复入库');
                }elseif($product_stock->status == ProductStockModel::STOCK_STATUS_OUT_SHOP){
                    // 这里是已出库，重新入库操作。类似调库。
                    $product_stock->status = ProductStockModel::STOCK_STATUS_IN_SHOP;
                    $product_stock->shop_id = $this->user['shop_id'];
                    $product_stock->update_time = time();
                    $product_stock->save();
                }else{
                    return $this->returnError([], sprintf("当前库存状态[%s]无法进行入库操作", $product_stock->status_str));
                }
            }else{
                if(empty($product_id)){
                    return $this->returnError([], '产品ID不能为空');
                }
                $data = [];
                $data['product_id'] = $product_id;
                $data['code'] = $code;
                $data['add_uid'] = $this->user['id'];
                $data['shop_id'] = $this->user['shop_id'];
                $data['update_time'] = time();
                $data['status'] = ProductStockModel::STOCK_STATUS_IN_SHOP;
                // 常规产品入库
                $data['create_time'] = time();
                $product_stock = ProductStockModel::create($data);
            }
            $option_id = ProductStockModel::STOCK_STATUS_IN_SHOP;
        }elseif($option_type == 2){

            if(!$product_stock){
                return $this->returnError([], '库存中不存在该商品，无法出库');
            }

            // 常规产品出库
            // 判断是不是在当前店铺中
            if($product_stock['shop_id'] != $this->user['shop_id']) return $this->returnError([], sprintf("该库存当前不在门店【%s】中，请使用【%s】员工账户进行出库", $this->user['shop']['shop_name'], $product_stock['shop']['shop_name']));
            // 这里进行出库操作。
            // 如果不是在库状态，不允许出库。
            if($product_stock->status != ProductStockModel::STOCK_STATUS_IN_SHOP) {
                return $this->returnError([], sprintf("当前库存状态[%s]无法进行出库操作", $product_stock->status_str));
            }
            $product_stock->status = ProductStockModel::STOCK_STATUS_OUT_SHOP;
            $product_stock->save();
            $option_id = ProductStockModel::STOCK_STATUS_OUT_SHOP;
        }elseif($option_type == 3){ // 标记售出。
            // 常规产品出库
            // 判断是不是在当前店铺中
            if(!$product_stock){
                return $this->returnError([], '库存中不存在该商品，无法出库');
            }
            if($product_stock['shop_id'] != $this->user['shop_id']) return $this->returnError([], sprintf(" 该库存当前不在门店【%s】中，请使用【%s】员工账户进行售出操作", $this->user['shop']['shop_name'], $product_stock['shop']['shop_name']));

            if($product_stock->status != ProductStockModel::STOCK_STATUS_IN_SHOP) {
                return $this->returnError([], sprintf("当前库存状态[%s]无法进行售出操作", $product_stock->status_str));
            }
            // 这里进行出库操作。
            $product_stock->status = ProductStockModel::STOCK_STATUS_SELE;
            $product_stock->save();
            $option_id = ProductStockModel::STOCK_STATUS_SELE;
        }
        $stock_id = $product_stock->id;
        $log_data = [
            'stock_id' => $stock_id,
            'option_id' => $option_id,
            'option_uid' => $option_uid,
            'option_shop_id' => $option_shop_id,
            'create_time' => time(),
        ];
        $productStockLogModel = new ProductStockLogModel();
        $product_stock = $productStockLogModel->insert($log_data);
        return $this->returnSuccess([], "操作成功");

    }


    // 获取产品列表
    public function list(){
        $status = input('status', 0, 'intval');
        $map = [];
        if($status){
            $map['status'] = $status;
        }
        $model = (new ProductStockModel());
        $result = $this->_query($model->where($map)->with(['shop', 'product', 'user', 'logs'])->order('id desc'), input())->like("code#keyword")->page(true, false);
        return $this->returnSuccess($result);
    }
}